package work.string;

public class Problem541 {
    public String reverseStr(String s, int k) {

        int head = 0;
        int tail;
        String ans = s;
        int n = s.length() / (2 * k);
        int m = s.length() % (2 * k);
        for (int i = 1; i <= n; i++) {
            tail = head + k - 1;
            ans = swap(ans, head, tail);
            head = i * 2 * k;
        }
        if (m < k && m != 0) {
            ans = swap(ans, head, s.length() - 1);
        } else if (m >= k && m != 0) {
            ans = swap(ans, head, head + k - 1);
        }
        return ans;
    }


    public String swap(String s, int i, int j) {
        char[] charArray = s.toCharArray();
        while (i < j) {
            char tmp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = tmp;
            i++;
            j--;
        }
        String s1 = new String(charArray);
        return s1;
    }

    public static void main(String[] args) {
        Problem541 problem541 = new Problem541();
        String abcdefg = problem541.reverseStr("krmyfshbspcgtexnnljhfursyissjnsocgdhgfxubewllxzqhpasguvlrxtkgatzfybprfmmfithphckksnvjkcvnsqgsgosfxc", 20);
        System.out.println(abcdefg);
    }
}
